package br.com.fox.iebav.controller.abstractcommand;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.primefaces.model.UploadedFile;


@Stateless
public abstract class AbstractCommand implements InterfaceCommand {

	
	public boolean uploadPDF(UploadedFile file, String nomeFile)
	{
		// metodo que resgata o path original da aplicacao ( abstract )
		String urlImage = prepararPathUpload("/web/files/pdf/"+nomeFile);
		// metodo que faz o upload ( abstract )
		return uploadFile(urlImage, file.getContents());
	}
	
	public boolean uploadIMAGES(UploadedFile file, String nomeFile)
	{
		// metodo que resgata o path original da aplicacao ( abstract )
		String urlImage = prepararPathUpload("/web/files/images/"+nomeFile);
		// metodo que faz o upload ( abstract )
		return uploadFile(urlImage, file.getContents());		
	}
	
	private String prepararPathUpload(String nomeArquivo)
	{
		//pega o diretorio que salvará na aplicacao
			FacesContext faces = FacesContext.getCurrentInstance();
			ServletContext servlet = (ServletContext) faces.getExternalContext().getContext();
			return servlet.getRealPath(nomeArquivo);
	}
	
	public boolean uploadFile(String path,byte[] dados)
	{
		// salva arquivo no disco
		FileOutputStream file;
		try 
		{
			file = new FileOutputStream(path);
			file.write(dados);
			file.flush();
			file.close();
			return true;	
		} catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean excluirFile(String path){
		
		boolean retorno = false;
		try {
				File file= new File(path); 
				// excluir arquivo
					if( file.delete() )
							retorno = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retorno;
	}
	
	protected String getValueMessageProperties(String key)
	{
		String res = "";
		
		//ler o arquivo
		Properties prop = new Properties();  
		  
		try 
		{
			prop.load( getClass().getResourceAsStream("/br/com/fox/iebav/utils/messages.properties") );
			res = prop.getProperty(key);
		}
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
	
	protected boolean validarEmail(String email)
	{
		  //Set the email pattern string  
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");  
	      //Match the given string with the pattern  
	    Matcher m = p.matcher(email);  
	      //check whether match is found   
	      boolean matchFound = m.matches();  
	  
	      return matchFound;
	}
	
	 //Função para criar hash da senha informada  
	   public static String md5(String senha) 
	   {  
		   try 
	       {  
			   MessageDigest md = MessageDigest.getInstance("MD5");  
	           BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));  
	           return  hash.toString(16);           
	       }
		   catch (NoSuchAlgorithmException  e)
	       {  
			   e.printStackTrace();  
	       	   return "null";
	       }  
	   }  
}
